package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.common.util.Base64Util;
import com.example.demo.common.util.DESUtil;
import com.example.demo.common.util.HttpClientUtil;
import com.example.demo.common.util.ResultVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.*;


@RestController
@RequestMapping("/api/callback")
public class AsyncNoticeController {
    private static final String PASS_WORD = "Pa2IpNKz";//朝歌测试
    @PostMapping("/sync")
    public String handle(@RequestHeader("PLATFORMCODE")String platformCode,//平台编号
                         @RequestHeader("REQCODE") String reqCode,//交易码
                         @RequestHeader("SERIALNO") String serialNo,//流水号，交易码+流水号 唯一确定一次请求
                         @RequestParam("data") String data){
        String decrypt = DESUtil.decrypt(PASS_WORD, data);//解密后的数据
        System.out.println("解密后的数据"+"------"+decrypt);
        if ("H1010".equals(reqCode)){
            Map<String, Object> selfMap = (Map<String, Object>) JSON.parse(decrypt);
            Map<String, String> licenseMap = (Map<String, String>) selfMap.get("LICENSE");
            if (!CollectionUtils.isEmpty(licenseMap)){
                String licenseData = licenseMap.get("DATA");
                String fileName = licenseMap.get("FILENAME");
                Base64Util.decode(licenseData,fileName+".jpg","D:\\crt_file\\image");
            }

            Map<String, String> enFileMap = (Map<String, String>) selfMap.get("ENFILE");
            if (!CollectionUtils.isEmpty(enFileMap)){
                String enFileData = enFileMap.get("DATA");
                String enFileName = enFileMap.get("FILENAME");
                Base64Util.decode(enFileData,enFileName,"D:\\crt_file\\image");
            }
        }else if("H1011".equals(reqCode)){
            Map<String, Object> generalInvoiceMap = (Map<String, Object>) JSON.parse(decrypt);
            String yn = (String) generalInvoiceMap.get("YN");
            if ("1".equals(yn)){
                Map<String, String> licenseMap = (Map<String, String>) generalInvoiceMap.get("INVOICE");
                String imgStr = licenseMap.get("DATA");
                String fileName = licenseMap.get("FILENAME");
                Base64Util.decode(imgStr,fileName+".jpg","D:\\crt_file\\image");
            }else {
                //发票废弃逻辑
            }
        }
        JSONObject result = new JSONObject();
        result.put("status",0);//0成功 1失败
        result.put("message","");//失败提示
        return JSON.toJSONString(result);
    }



}
